import axios from 'axios'
import { ElMessage } from 'element-plus'

const instance = axios.create({
  timeout: 10000,
  withCredentials: true
})

instance.interceptors.request.use(config => {
  // 设置拦截器，登录后给token
  //config.headers['x-auth-token'] = window.sessionStorage.getItem('token')
  return config
}, (error) => {
  if (error.message.indexOf('timeout') !== -1) { // 如果连接超时的话
    ElMessage.error('连接超时！')
  }
  return Promise.reject(error)
})

instance.interceptors.response.use(
  response => response,
  error => {
    ElMessage.error(error.message || '请求失败')
    return Promise.reject(error)
  }
)

export default instance